package com.pj.wisdomlogistics.ui.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.CarPoritionInfo;
import com.pj.wisdomlogistics.mvp.contract.CarPoritionContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.CarPoritionPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.CarPagerAdapter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CarPoritionActivity extends MvpActivity<CarPoritionPresenter> implements CarPoritionContract.View {

    @BindView(R.id.tabs)
    TabLayout mTabs;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    private int selectCarPos = -1;
    private int selectDriverPos = -1;
    private int carApplyId = -1;

    private List<CarPoritionInfo.CarListBean> mCarList;
    private List<CarPoritionInfo.DriverListBean> mDriverList;

    @Override
    public void onError(String errorMsg) {
        CustomToast.showToast(this , errorMsg , 1000);
    }

    @Override
    public void onSuccess(CarPoritionInfo mInfo) {
        mCarList = mInfo.getCarList();
        mDriverList = mInfo.getDriverList();
        EventBus.getDefault().post(
                new EventCenter<>(Cons.CarInfo.CAR_CHOOSE , mCarList));
        EventBus.getDefault().post(
                new EventCenter<>(Cons.CarInfo.DRIVER_CHOOSE , mDriverList));
    }

    @Override
    public void onSendSuccess() {
        dismissProgressDialog();
        CustomToast.showToast(this , "分配成功" , 1000);
        EventBus.getDefault().post(new EventCenter<>(Cons.CarInfo.SURE_OK));
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        mPresenter.findCarAndDriver();
        mViewPager.setAdapter(new CarPagerAdapter(getSupportFragmentManager()));
        mViewPager.setOffscreenPageLimit(2);
        mTabs.setupWithViewPager(mViewPager);

        carApplyId = getIntent().getIntExtra(Cons.CarInfo.CAR_PORITION , -1);
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_car_porition;
    }

    @Override
    public boolean isApplyEventBus() {
        return true;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
        if(eventCenter.getEventCode() == Cons.CarInfo.CAR_CHOOSE_OK){
            selectCarPos = (int) eventCenter.getData();
        }else if(eventCenter.getEventCode() == Cons.CarInfo.DRIVER_CHOOSE_OK){
            selectDriverPos = (int) eventCenter.getData();
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.tv_sure:
                if(selectDriverPos == -1 || selectCarPos == -1){
                    CustomToast.showToast(this , "请选择司机及车辆" , 1000);
                    return;
                }
                mPresenter.carApplyAllot(
                        MyApplication.userID ,
                        carApplyId + "",
                        4 + "",
                        mDriverList.get(selectDriverPos).getDriverName() ,
                        mDriverList.get(selectDriverPos).getDriverTel() + "" ,
                        mCarList.get(selectCarPos).getCarModel() ,
                        mCarList.get(selectCarPos).getCarNum() ,
                        mDriverList.get(selectDriverPos).getDriverID() ,
                        mCarList.get(selectCarPos).getCarID() + "" ,
                        ""
                );
                showProgressDialog();
                break;
        }
    }
}
